package com.jc.repository.generator;

/**
 * @author wangli
 * @create 2016-04-27 下午3:50
 * @description
 */


import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.api.ProgressCallback;
import org.mybatis.generator.api.VerboseProgressCallback;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static org.mybatis.generator.internal.util.messages.Messages.getString;


public class GenerateMybatis {

    //	private static final String CONFIG_FILE = "-configfile"; //$NON-NLS-1$
    //	private static final String OVERWRITE = "-overwrite"; //$NON-NLS-1$
    //	private static final String CONTEXT_IDS = "-contextids"; //$NON-NLS-1$
    //	private static final String TABLES = "-tables"; //$NON-NLS-1$
    //	private static final String VERBOSE = "-verbose"; //$NON-NLS-1$
    //	private static final String FORCE_JAVA_LOGGING = "-forceJavaLogging"; //$NON-NLS-1$
    //	private static final String HELP_1 = "-?"; //$NON-NLS-1$
    // private static final String HELP_2 = "-h"; // $NON-N

    //	private static final String XML_NAME = "dt-user-behavior-generatorConfig.xml";
//    private static final String XML_NAME = "jc-core-generatorConfig.xml";


//    private String XML_NAME;
//    private String config_dir;
//
//    public GenerateMybatis(String XML_NAME, String config_dir) {
//        this.XML_NAME = XML_NAME;
//        this.config_dir = config_dir;
//    }

    /**
     * @param args
     */
//    public static void main(String[] args) {
//    public static void generator() {
    public static void generator(String config_dir, String XML_NAME) {



//        String XML_NAME = "jc-core-generatorConfig.xml";
//        String XML_NAME = "/Users/jcm002/wangli/code/jc-java-wangli/datasource-test/src/main/resources/jc-core-generatorConfig.xml";
//        String config_dir = GenerateMybatis.class.getClassLoader().getResource("generate").getFile();
//        config_dir = "/Users/jcm002/wangli/code/jc-java-wangli/datasource-test/src/main/resources/";

        



        List<String> warnings = new ArrayList<String>();
        Set<String> fullyqualifiedTables = new HashSet<String>();

        Set<String> contexts = new HashSet<String>();
        try {
            for (File configurationFile : new File(config_dir).listFiles()) {
                System.out.println(configurationFile.getName());
                if(configurationFile.getName().equals(XML_NAME)) {
                    ConfigurationParser cp = new ConfigurationParser(warnings);
                    Configuration config = cp.parseConfiguration(configurationFile);

                    DefaultShellCallback shellCallback = new DefaultShellCallback(Boolean.TRUE);

                    MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, shellCallback, warnings);

                    ProgressCallback progressCallback = new VerboseProgressCallback();

                    myBatisGenerator.generate(progressCallback, contexts, fullyqualifiedTables);
                }
            }
        } catch (XMLParserException e) {
            writeLine(getString("Progress.3")); //$NON-NLS-1$
            writeLine();
            for (String error : e.getErrors()) {
                writeLine(error);
            }

            return;
        } catch (SQLException e) {
            e.printStackTrace();
            return;
        } catch (IOException e) {
            e.printStackTrace();
            return;
        } catch (InvalidConfigurationException e) {
            writeLine(getString("Progress.16")); //$NON-NLS-1$
            for (String error : e.getErrors()) {
                writeLine(error);
            }
            return;
        } catch (InterruptedException e) {
            // ignore (will never happen with the DefaultShellCallback)
            ;
        }

        for (String warning : warnings) {
            writeLine(warning);
        }

        if (warnings.size() == 0) {
            writeLine(getString("Progress.4")); //$NON-NLS-1$
        } else {
            writeLine();
            writeLine(getString("Progress.5")); //$NON-NLS-1$
        }
    }

    private static void writeLine(String message) {
        System.out.println(message);
    }

    private static void writeLine() {
        System.out.println();
    }

}

